package com.cnpinxi.controller.admin.order;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.*;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/admin/order")
public class AdminOrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private PaymentService paymentService;
 @Autowired
    private RefundService refundService;
 @Autowired
    private ExpressInfoService expressInfoService;
    @Autowired
    private CommissionService commissionService;



    /**
     * 支付提成
     * @param tid
     * @return
     */
    @RequestMapping(value = "/payCommission")
    @ResponseBody
    public String payCommission(Long tid) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {

       commissionService.payCommission(tid);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    /**
     * 提成列表
     * @param request
     * @return
     */
    @RequestMapping("/commissionList")
    public ModelAndView commissionList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/order/commissionList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Commission> commissionList = commissionService.selectCommissionList(map);
        PageInfo<Commission> pageinfo = new PageInfo<>(commissionList);
        //数据汇总map
        Map<String,Object> resultMap = commissionService.selectCommissionSum(map);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        m.addObject("resultMap",resultMap);
        return m;
    }
    /**
     * 经理提成列表
     * @param request
     * @return
     */
    @RequestMapping("/managerCommissionList")
    public ModelAndView managerCommissionList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/order/managerCommissionList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<ManagerCommission> managerCommissionList = commissionService.selectManagerCommissionList(map);
        PageInfo<ManagerCommission> pageinfo = new PageInfo<>(managerCommissionList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }


    /**
     * 查询订单列表
     * @param request
     * @return
     */
    @RequestMapping("/getOrderList")
    public ModelAndView getOrderList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/order/getOrderList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Order> orderList = orderService.getOrderList(map);
        PageInfo<Order> pageinfo = new PageInfo<>(orderList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }
  /**
     * 添加物流
     * @param orderId
     * @return
     */
    @RequestMapping("/logistics")
    public ModelAndView logistics(Long orderId) {
        ModelAndView m = new ModelAndView("/admin/order/logistics");

      //OrderItem orderItem=orderItemService.getOrderItemByOrderItemId(orderId);
      Order order = orderService.getOrderById(orderId);

     Map<String,String> map=new HashMap<>();
     map.put("orderId",String.valueOf(orderId));
     ExpressInfo expressInfo=expressInfoService.selectExpressInfo(map);
     m.addObject("order",order);
     m.addObject("expressInfo",expressInfo);
     return m;
    }


    /**
     * 查询订单产品详情
     * @param request
     * @return
     */
    @RequestMapping("/getOrderItemList")
    public ModelAndView getOrderItemList(HttpServletRequest request, Integer page, Integer pageSize,Long orderId) {
        ModelAndView m = new ModelAndView("/admin/order/getOrderItemList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        map.put("orderId",String.valueOf(orderId));
        PageHelper.startPage(page, pageSize);
        List<OrderItem> orderItemList = orderItemService.selectOrderItemList(map);
        PageInfo<OrderItem> pageinfo = new PageInfo<>(orderItemList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }
    /**
     * 查询订单支付详情
     * @param request
     * @return
     */
    @RequestMapping("/getPayMentList")
    public ModelAndView getPayMentList(HttpServletRequest request, Long orderId) {
        ModelAndView m = new ModelAndView("/admin/order/getPayMentList");
        Map<String, String> map = this.showRequestParameters(request);
       /* if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));*/
        map.put("orderId",String.valueOf(orderId));
        List<Payment> payMentList = paymentService.selectpaymentListByOrderId(map);
        PageInfo<Payment> pageinfo = new PageInfo<>(payMentList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }

    /**
     * 订单退款状态更新
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/modifyOrderStatus")
    public void modifyOrderItemStatus(Long orderId){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            orderService.modifyOrderStatus( orderId, OrderService.REFUNDED);
            resultObj.setMsg("操作成功");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("resultObj====>>>>"+resultObj.toJson());
    }


    /**
     * 退款确认
     * @param orderItemId
     * @return
     */
    @RequestMapping(value = "/refundConfirm", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String refundConfirm(Long orderItemId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {

            if (!refundService.refundConfirm(orderItemId)) {
                resultObj.setCode(ResultObj.CODE_FAIL);
                resultObj.setMsg("退款失败！");
            }
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
